﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Resources;
using System.Collections;
using System.Diagnostics;

namespace ResourceTranslator
{
    /// <summary>
    /// Interaction logic for ResourceViewer.xaml
    /// </summary>
    public partial class ResourceViewer : UserControl
    {
        public ResourceViewer()
        {
            InitializeComponent();
        }


        void PopulateGrid(Grid grid, ResourceData[] resources)
        {
            grid.Children.Clear();
            grid.RowDefinitions.Clear();

            if (resources == null)
            {
                return;
            }

            AddRow(grid, new ResourceData { Key = "Name", Value = "Value", Comments = "Comments" }, true);

            foreach (ResourceData resource in resources)
            {
                AddRow(grid, resource, false);
            }
            AddSplitter(grid);
        }

        void AddRow(Grid grid, ResourceData resource, bool header)
        {
            int rowCount = grid.RowDefinitions.Count;

            FrameworkElement key;
            FrameworkElement value;
            if (header)
            {
                TextBlock tb = new TextBlock();
                //tb.Text =  resource.Key;
                tb.DataContext = resource;
                tb.SetBinding(TextBlock.TextProperty, "Key");
                key = tb;

                TextBlock tb1 = new TextBlock();
                //tb1.Text = resource.Value;
                tb1.DataContext = resource;
                tb1.SetBinding(TextBlock.TextProperty, "Value");
                value = tb1;
            }
            else
            {
                TextBox tb = new TextBox();
                //tb.Text = resource.Key;
                tb.DataContext = resource;
                tb.SetBinding(TextBox.TextProperty, "Key");
                key = tb;

                TextBox tb1 = new TextBox();
                //tb1.Text = resource.Value;
                tb1.DataContext = resource;
                tb1.SetBinding(TextBox.TextProperty, "Value");
                value = tb1;
            }
            
            key.MinWidth = 30;
            key.Margin = new Thickness(3);
            Grid.SetRow(key, rowCount);
            Grid.SetColumn(key, 0);

            value.DataContext = resource;
            value.MinWidth = 30;
            value.Margin = new Thickness(3);
            Grid.SetRow(value, rowCount);
            Grid.SetColumn(value, 1);

            RowDefinition rd = new RowDefinition();
            rd.Height = GridLength.Auto;
            grid.RowDefinitions.Add(rd);
            grid.Children.Add(key);
            grid.Children.Add(value);
        }
        void AddSplitter(Grid grid)
        {
            GridSplitter splitter = new GridSplitter();
            splitter.HorizontalAlignment = HorizontalAlignment.Right;
            splitter.VerticalAlignment = VerticalAlignment.Stretch;
            splitter.Width = 3;
            splitter.Background = new SolidColorBrush(Colors.AliceBlue);
            Grid.SetColumn(splitter, 0);
            Grid.SetRow(splitter, 0);
            Grid.SetRowSpan(splitter, grid.RowDefinitions.Count);
            grid.Children.Add(splitter);
        }

        ResourceData[] currentResources;
        public ResourceData[] ResourcesData
        {
            get
            {
                return currentResources;
            }
            set
            {
                currentResources = value;

                PopulateGrid(resourcesGrid, currentResources);
            }
        }

        public void LoadResourceFile(string file)
        {
            ResourceFileType resFileType = GetResourceFileType(file);

            switch (resFileType)
            {
                case ResourceFileType.Text:
                    ResourcesData = ResourcesUtil.ReadTextResources(file);
                    break;
                case ResourceFileType.Resx:
                    ResourcesData = ResourcesUtil.ReadResxResources(file);
                    break;
                case ResourceFileType.BinaryResources:
                    ResourcesData = ResourcesUtil.ReadBinaryResources(file);
                    break;
                default:
                    throw new NotSupportedException("File type not supported!");
            }
        }


        public void SaveResourceFile(string file)
        {
            if (currentResources == null || currentResources.Length == 0)
                throw new InvalidOperationException("No data to save");

            ResourceFileType resFileType = GetResourceFileType(file);

            switch (resFileType)
            {
                case ResourceFileType.Text:
                    ResourcesUtil.WriteTextResources(currentResources, file);
                    break;
                case ResourceFileType.Resx:
                    ResourcesUtil.WriteResxResources(currentResources, file);
                    break;
                case ResourceFileType.BinaryResources:
                    ResourcesUtil.WriteBinaryResources(currentResources, file);
                    break;
                default:
                    throw new NotSupportedException("File type not supported!");
            }
        }

        ResourceFileType GetResourceFileType(string file)
        {
            FileInfo fileInfo = new FileInfo(file);

            string extension = fileInfo.Extension;
            if (String.Equals(extension, ".txt", StringComparison.InvariantCultureIgnoreCase))
                return ResourceFileType.Text;
            if (String.Equals(extension, ".resources", StringComparison.InvariantCultureIgnoreCase))
                return ResourceFileType.BinaryResources;
            if (String.Equals(extension, ".resx", StringComparison.InvariantCultureIgnoreCase))
                return ResourceFileType.Resx;

            return ResourceFileType.UnKnown;
        }

        enum ResourceFileType
        {
            Text,
            Resx,
            BinaryResources,
            UnKnown
        }
    }
}
